<template>
  <div>
    <el-row justify="end" type="flex">
      <el-select v-model="currentYear" size="small" placeholder="请选择年份" style="width: 120px" @change="currentChange">
        <el-option
          v-for="item in yearData"
          :key="item"
          :value="item"
        />
      </el-select>
      <el-select v-model="currentMonth" size="small" placeholder="请选择月份" style="width: 120px;margin-left:10px" @change="currentChange">
        <el-option
          v-for="item in 12"
          :key="item"
          :value="item"
        />
      </el-select>
    </el-row>
    <el-row>
      <el-calendar v-model="calendarValue">
        <template v-slot:dateCell="{date,data}" class="content">
          <div class="date-content">
            <span class="text"> {{ handlerDate(data.day) }}</span>
            <span v-if="isWeekend(date)" class="rest">休</span>
          </div>
        </template>
      </el-calendar>
    </el-row>

  </div>
</template>

<script>
export default {
  data() {
    return {
      currentMonth: null,
      currentYear: null,
      calendarValue: new Date(),
      yearData: []
    }
  },
  created() {
    this.yearDataList()
    this.currentMonth = new Date().getMonth() + 1
    this.currentYear = new Date().getFullYear()
  },
  methods: {
    yearDataList() {
      const year = new Date().getFullYear()
      // Array.from 里面的回调函数第一个参数a是前面一个数组,返回的值会赋值给新的数组
      this.yearData = Array.from(Array(11), (a, index) => {
        return year - 5 + index
      })
    },
    currentChange() {
      this.calendarValue = new Date(`${this.currentYear}-${this.currentMonth}-1`)
    },
    handlerDate(date) {
      const res = date.split('-')[2]
      return res.startsWith('0') ? res.substr(1) : res
    },
    isWeekend(data) {
      return data.getDay() === 6 || data.getDay() === 0
    }
  }
}
</script>

<style scoped lang="scss">
::v-deep .el-calendar-day {
  height:  auto;
 }
  ::v-deep .el-calendar-table__row td,::v-deep .el-calendar-table tr td:first-child,  ::v-deep .el-calendar-table__row td.prev{
  border:none;
 }
 .date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 ::v-deep.el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 ::v-deep.el-calendar__header {
   display: none
 }
</style>
